{% extends "documents/document_base.html" %}
{% load i18n %}
{% load dialogos_tags %}
{% load pinax_ratings_tags %}
{% load bootstrap_tags %}
{% load base_tags %}
{% load guardian_tags %}
{% load document_tags %}

{% block title %}{{ resource.title }} — {{ block.super }}{% endblock %}

{% block head %}

  {% if TWITTER_CARD %}
    {% include "base/_resourcebase_twittercard.html" %}
  {% endif %}
  {% if OPENGRAPH_ENABLED %}
    {% include "base/_resourcebase_opengraph.html" %}
  {% endif %}

{{ block.super }}
{% endblock %}

{% block body_class %}documents{% endblock %}

{% block body_outer %}

<div class="page-header">
  <h2>{{ resource.title }}</h2>
</div>

<div class="row">
  <div class="col-md-8">
    {% if "download_resourcebase" in perms_list %}
      {% if resource.extension|lower in audiotypes and resource.doc_file %}
        {% get_mime_type mimetypemap resource.extension as mimetype %}
        <div id="embedded_map" width="500px">
            <a style="text-decoration:none;" href="{% url "document_link" resource.id %}" target="_blank">
                <audio controls width="100%">
                    <source src="{% url "document_download" resource.id %}" type="{{ mimetype }}">
                    {% trans "Your browser does not support the audio element." %}
                </audio>
            </a>
        </div>
      {% elif resource.extension|lower in imgtypes and resource.doc_file %}
        <div id="embedded_map" width="500px">
            <a style="text-decoration:none;" href="{% url "document_link" resource.id %}" target="_blank">
                <img src="{% url "document_download" resource.id %}" width='100%' class="img-responsive" />
            </a>
        </div>
      {% elif resource.extension|lower in videotypes and resource.doc_file %}
        {% get_mime_type mimetypemap resource.extension as mimetype %}
        <div id="embedded_map" width="500px">
            <a style="text-decoration:none;" href="{% url "document_link" resource.id %}" target="_blank">
                <video width="500" controls>
                    <source src="{% url "document_download" resource.id %}" type="{{ mimetype }}">
                    {% trans "Your browser does not support the video tag." %}
                </video>
            </a>
        </div>
      {% elif resource.doc_file %}
      <p><a href="{% url "document_link" resource.id %}" target="_blank">{% trans "Download the" %} {{ resource }} {% trans "document" %}</a></p>
      {% elif  resource.doc_url %}
      <p><a href="{{ resource.doc_url }}" target="_blank">{% trans "Download the" %} {{ resource }} {% trans "document" %}.</a> <small>({% trans 'External Resource' %})</small></p>
      {%  endif %}
    {%  endif %}

    <div class="documents-actions">
      {% include "_actions.html" %}
    </div>

    <div class="tab-content">
      <article id="info" class="tab-pane{% if tab == 'info' or not tab %} active{% endif %}">
      {% include "base/resourcebase_info_panel.html" %}
      </article>

      {% block social_links %}
        {% if DISPLAY_SOCIAL %}
        {% include "social_links.html" %}
        {% endif %}
      {% endblock %}

      {% if DISPLAY_COMMENTS %}
      <article id="comments" class="tab-pane">
      {% include "_comments.html" %}
      </article>
      {% endif %}

      {% if DISPLAY_RATINGS %}
      <article id="rate" class="tab-pane">
        <!-- TODO: Move this to a reusable template snippet -->
        {% if request.user.is_authenticated %}
        <h4>{% trans "Rate this document" %}</h4>
        {% user_rating request.user resource "document" as user_document_rating %}
        <div id="user_rating" class="category-layer" data-score="{{user_document_rating}}"></div>
        {% endif %}
        <h4>{% trans 'Average Rating' %}</h4>
        {% overall_rating resource "document" as document_rating %}
        {% num_ratings resource as num_votes %}
        <div class="overall_rating" style="float:left" data-score="{{ document_rating }}"></div> ({{num_votes}})
        <!-- TODO: Add display of who gave what rating based -->
      </article>
      {% endif %}

      {% if EXIF_ENABLED and exif_data %}
        {% with "exif/_exif_document_detail.html" as exif_template %}
            {% include exif_template %}
        {% endwith %}
      {% endif %}

      {% if FAVORITE_ENABLED %}
      <article class="tab-pane" id="favorite">
        {% include "favorite/_favorite.html" %}
      </article>
      {% endif %}

    </div>
  </div>

  <div class="col-md-4">

    <li class="list-group-item">
        <a href="{% url "document_metadata_detail" resource.id %}">
            <button class="btn btn-primary btn-md btn-block">{% trans "Metadata Detail" %}</button>
        </a>
    </li>

    <ul class="list-group">
      <li class="list-group-item">
      {% if "download_resourcebase" in perms_list %}
        {% if resource.extension|lower in imgtypes and resource.doc_file %}
          <a style="text-decoration:none;" target="_blank" href="{% url "document_download" resource.id %}"><button class="btn btn-primary btn-md btn-block">{% trans "Download Document" %}</button></a>
        {% elif resource.doc_file %}
          <a style="text-decoration:none;" target="_blank" href="{% url "document_download" resource.id %}"><button class="btn btn-primary btn-md btn-block">{% trans "Download Document" %}</button></a>
        {% elif  resource.doc_url %}
          <a style="text-decoration:none;" target="_blank" href="{{ resource.doc_url }}"><button class="btn btn-primary btn-md btn-block">{% trans "Download Document" %}</button></a>
        {%  endif %}
      {% else %}
        {% if request.user.is_authenticated %}
        <button class="btn btn-primary btn-md btn-block" id="request-download">{% trans "Request Download" %}</button>
        {%  endif %}
      {%  endif %}
      </li>
      {% if not READ_ONLY_MODE %}
        {% if "change_resourcebase_metadata" in perms_list or "change_resourcebase" in perms_list or "delete_resourcebase" in perms_list %}
          <li class="list-group-item">
            <button class="btn btn-primary btn-md btn-block" data-toggle="modal" data-target="#edit-document">{% trans "Edit Document" %}</button>
          </li>
        {% endif %}
      {% endif %}
        {% display_edit_request_button resource request.user perms_list as display_request_button %}
        {% if display_request_button %}
            <li class="list-group-item">
                <a href="{% url "owner_rights_request" resource.id %}">
                    <button class="btn btn-primary btn-md btn-block">{% trans "Request change" %}</button>
                </a>
            </li>
        {% endif %}
      <div class="modal fade" id="edit-document" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
              <h4 class="modal-title" id="myModalLabel">{% trans "Edit Document" %}</h4>
            </div>
            <div class="modal-body">

                <div class="row edit-modal">
                  {% if "change_resourcebase_metadata" in perms_list %}
                  <div class="col-sm-3">
                    <i class="fa fa-list-alt fa-3x"></i>
                    <h4>{% trans "Metadata" %}</h4>
                    <a class="btn btn-default btn-block btn-xs" href="{% url "document_metadata" resource.id %}">{% trans "Wizard" %}</a>
                    <a class="btn btn-default btn-block btn-xs" href="{% url "document_metadata_advanced" resource.id %}">{% trans "Advanced Edit" %}</a>
                  </div>
                  {% endif %}
                  {% if "change_resourcebase" in perms_list %}
                  <div class="col-sm-3">
                    <i class="fa fa-photo fa-3x"></i>
                    <h4>{% trans "Thumbnail" %}</h4>
                    <a class="btn btn-default btn-block btn-xs" href="{% url "thumbnail_upload" resource.resourcebase_ptr.id %}">{% trans "Upload" %}</a>
                  </div>
                  {% endif %}
                  {% if "change_resourcebase" in perms_list or "delete_resourcebase" in perms_list %}
                  <div class="col-sm-3">
                    <i class="fa fa-file-text-o fa-3x"></i>
                    <h4>{% trans "Document" %}</h4>
                    {% if "change_resourcebase" in perms_list %}
                    <a class="btn btn-default btn-block btn-xs" href="{% url "document_replace" resource.id %}">{% trans "Replace" %}</a>
                    {% endif %}
                    {% if "delete_resourcebase" in perms_list %}
                    <a class="btn btn-danger btn-block btn-xs" href="{% url "document_remove" resource.id %}">{% trans "Remove" %}</a>
                    {% endif %}
                  </div>
                  {% endif %}
                </div>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">{% trans "Close" %}</button>
              </div>
            </div>
          </div>
        </div>

      <li class="list-group-item">
        <button class="btn btn-default btn-md btn-block" data-toggle="modal" data-target="#download-metadata">{% trans "Download Metadata" %}</button>
      </li>
      <div class="modal fade" id="download-metadata" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
              <h4 class="modal-title" id="myModalLabel">{% trans "Download Metadata" %}</h4>
            </div>
            <div class="modal-body">
			    <div style="margin-bottom:20px">
					<h5 class="modal-title" id="myModalLabel">
					{% trans "Full metadata" %}
					</h5>
					<ul style="list-style: outside none none;padding: 0;">
						<li><a target="_blank" href="../catalogue/csw_to_extra_format/{{resource.uuid}}/{{resource.title}}.txt"> {% trans "Text format" %} </a></li>
						<li><a target="_blank" href="../catalogue/csw_to_extra_format/{{resource.uuid}}/{{resource.title}}.html"> {% trans "HTML format" %} </a></li>
					</ul>
				</div>
			  <h5 class="modal-title" id="myModalLabel">
			  {% trans "Standard Metadata - XML format" %}
			  </h5>
              <ul style="list-style: outside none none;padding: 0;">
                {% for link in metadata %}
                <li><a href="{{ link.url }}">{{ link.name }}</a></li>
                {% endfor %}
              </ul>
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">{% trans "Close" %}</button>
            </div>
          </div>
        </div>
      </div>

      <li class="list-group-item">
        <h4>{% trans "Resources using this document" %}</h4>
        {% if layer.maps %}
        <p>{% trans "List of resources using this document:" %}</p>
        {% endif %}
        <ul class="list-unstyled">
          {% for resource in related %}
          <li><a href="{{ resource.get_absolute_url }}">{{ resource.title }}</a></li>
          {% empty %}
          <p>{% trans "This document is not related to any maps or layers" %}</p>
          {% endfor %}
        </ul>
      </li>

      {% if GEONODE_SECURITY_ENABLED %}
        {% if "change_resourcebase_permissions" in perms_list %}
          {% if not READ_ONLY_MODE %}
            <li class="list-group-item">
              <h4>{% trans "Permissions" %}</h4>
              <p>{% trans "Click the button below to change the permissions of this document." %}</p>
              <p><a href="#modal_perms" data-toggle="modal" class="btn btn-primary btn-block" data-target="#_permissions">{% trans "Change Document Permissions" %}</a></p>
            </li>
            {% include "_permissions_form.html" %}
          {% endif %}
        {% endif %}
      {% endif %}

      {% include "base/_resourcebase_contact_snippet.html" %}

    </ul>
  </div>
</div>
{% endblock %}

{% block extra_script %}
{{ block.super }}
{% if DISPLAY_SOCIAL %}
    {% include 'facebook_sdk.html' %}
{% endif %}
    {% if request.user.is_authenticated %}
        {% user_rating_js request.user resource "document" %}
    {% else %}
        {% overall_rating resource "document" as the_doc_rating %}
    {% endif %}
    {% include 'rating.html' %}
    {% include 'request_download.html' %}
    {% include "_permissions_form_js.html" %}

    {% if FAVORITE_ENABLED %}
    {% include "favorite/_favorite_js.html" %}
    {% endif %}
{% endblock extra_script %}
